package edu.whu.rose.clock.kps.spindex;

import java.util.Arrays;

import edu.whu.rose.clock.ssql.graph.RRadiusSubGraph;

public class ShortestPathSearch {

	public static ShortestPathInSubgraph run(RRadiusSubGraph rrsg) {
		int[][] distances = new int[rrsg.getVertexNumber()][rrsg.getVertexNumber()];
		int[][] predecessors = new int[rrsg.getVertexNumber()][rrsg.getVertexNumber()];
		
		for (int i = 0; i < rrsg.getVertexNumber(); i++) {
			Arrays.fill(distances[i], Integer.MAX_VALUE);
			distances[i][i] = 0;
		}

		if (rrsg.getStartVertexes() == null) {
			System.err.println(rrsg.getVertexNumber());
			System.err.println(rrsg.getVertexes()[0]);
			ShortestPathInSubgraph spis = new ShortestPathInSubgraph();
			spis.setDistances(distances);
			spis.setPredecessors(predecessors);
			return spis;
		}
		
		for (int i = 0; i < rrsg.getStartVertexes().length; i++) {
			int start = Arrays.binarySearch(rrsg.getVertexes(), rrsg.getStartVertexes()[i]);
			int end = Arrays.binarySearch(rrsg.getVertexes(), rrsg.getEndVertexes()[i]);
			distances[start][end] = 1;
			predecessors[start][end] = start + 1;
			distances[end][start] = 1;
			predecessors[end][start] = end + 1;
		}
		
		return run(distances, predecessors);
	}
	
	public static ShortestPathInSubgraph run(int[][] distances, int[][] predecessors) {
		ShortestPathInSubgraph spis = new ShortestPathInSubgraph();
		
		FloydWarshallAlgorithm.process(distances, predecessors);
		
		spis.setDistances(distances);
		spis.setPredecessors(predecessors);
		
		return spis;
	}
}
